/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fun.tan90.device.sink.mqtt.plugin.config;

import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.codec.MqttMessageBuilders;
import org.dromara.mica.mqtt.codec.MqttProperties;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.core.client.IMqttClientMessageListener;
import org.dromara.mica.mqtt.core.client.MqttClient;
import org.dromara.mica.mqtt.core.client.MqttClientSubscription;

import java.util.List;
import java.util.function.Consumer;

/**
 * mqtt client 模板
 *
 * @author wsq（冷月宫主）
 * @author ChangJin Wei (魏昌进)
 */
@Slf4j
public class MqttClientTemplate {

    private final MqttClient client;

    public MqttClientTemplate(MqttClient mqttClient) {
        this.client = mqttClient;
    }

    /**
     * 订阅
     *
     * @param topicFilter topicFilter
     * @param listener    MqttMessageListener
     * @return MqttClient
     */
    public MqttClient subQos0(String topicFilter, IMqttClientMessageListener listener) {
        return client.subscribe(topicFilter, MqttQoS.QOS0, listener);
    }

    /**
     * 订阅
     *
     * @param topicFilter topicFilter
     * @param listener    MqttMessageListener
     * @return MqttClient
     */
    public MqttClient subQos1(String topicFilter, IMqttClientMessageListener listener) {
        return client.subscribe(topicFilter, MqttQoS.QOS1, listener);
    }

    /**
     * 订阅
     *
     * @param topicFilter topicFilter
     * @param listener    MqttMessageListener
     * @return MqttClient
     */
    public MqttClient subQos2(String topicFilter, IMqttClientMessageListener listener) {
        return client.subscribe(topicFilter, MqttQoS.QOS2, listener);
    }

    /**
     * 订阅
     *
     * @param mqttQoS     MqttQoS
     * @param topicFilter topicFilter
     * @param listener    MqttMessageListener
     * @return MqttClient
     */
    public MqttClient subscribe(MqttQoS mqttQoS, String topicFilter, IMqttClientMessageListener listener) {
        return client.subscribe(mqttQoS, topicFilter, listener);
    }

    /**
     * 订阅
     *
     * @param mqttQoS     MqttQoS
     * @param topicFilter topicFilter
     * @param listener    MqttMessageListener
     * @return MqttClient
     */
    public MqttClient subscribe(String topicFilter, MqttQoS mqttQoS, IMqttClientMessageListener listener) {
        return client.subscribe(topicFilter, mqttQoS, listener);
    }

    /**
     * 订阅
     *
     * @param mqttQoS     MqttQoS
     * @param topicFilter topicFilter
     * @param listener    MqttMessageListener
     * @param properties  MqttProperties
     * @return MqttClient
     */
    public MqttClient subscribe(String topicFilter, MqttQoS mqttQoS, IMqttClientMessageListener listener, MqttProperties properties) {
        return client.subscribe(topicFilter, mqttQoS, listener, properties);
    }

    /**
     * 订阅
     *
     * @param topicFilters topicFilter 数组
     * @param mqttQoS      MqttQoS
     * @param listener     MqttMessageListener
     * @return MqttClient
     */
    public MqttClient subscribe(String[] topicFilters, MqttQoS mqttQoS, IMqttClientMessageListener listener) {
        return client.subscribe(topicFilters, mqttQoS, listener);
    }

    /**
     * 订阅
     *
     * @param topicFilters topicFilter 数组
     * @param mqttQoS      MqttQoS
     * @param listener     MqttMessageListener
     * @param properties   MqttProperties
     * @return MqttClient
     */
    public MqttClient subscribe(String[] topicFilters, MqttQoS mqttQoS, IMqttClientMessageListener listener, MqttProperties properties) {
        return client.subscribe(topicFilters, mqttQoS, listener, properties);
    }

    /**
     * 批量订阅
     *
     * @param subscriptionList 订阅集合
     * @return MqttClient
     */
    public MqttClient subscribe(List<MqttClientSubscription> subscriptionList) {
        return client.subscribe(subscriptionList);
    }

    /**
     * 批量订阅
     *
     * @param subscriptionList 订阅集合
     * @param properties       MqttProperties
     * @return MqttClient
     */
    public MqttClient subscribe(List<MqttClientSubscription> subscriptionList, MqttProperties properties) {
        return client.subscribe(subscriptionList, properties);
    }

    /**
     * 取消订阅
     *
     * @param topicFilters topicFilter 集合
     * @return MqttClient
     */
    public MqttClient unSubscribe(String... topicFilters) {
        return client.unSubscribe(topicFilters);
    }

    /**
     * 取消订阅
     *
     * @param topicFilters topicFilter 集合
     * @return MqttClient
     */
    public MqttClient unSubscribe(List<String> topicFilters) {
        return client.unSubscribe(topicFilters);
    }

    /**
     * 发布消息
     *
     * @param topic   topic
     * @param payload 消息内容
     * @return 是否发送成功
     */
    public boolean publish(String topic, Object payload) {
        return client.publish(topic, payload, MqttQoS.QOS0);
    }

    /**
     * 发布消息
     *
     * @param topic   topic
     * @param payload 消息内容
     * @param qos     MqttQoS
     * @return 是否发送成功
     */
    public boolean publish(String topic, Object payload, MqttQoS qos) {
        return client.publish(topic, payload, qos);
    }

    /**
     * 发布消息
     *
     * @param topic   topic
     * @param payload 消息内容
     * @param retain  是否在服务器上保留消息
     * @return 是否发送成功
     */
    public boolean publish(String topic, Object payload, boolean retain) {
        return client.publish(topic, payload, retain);
    }

    /**
     * 发布消息
     *
     * @param topic   topic
     * @param payload 消息体
     * @param qos     MqttQoS
     * @param retain  是否在服务器上保留消息
     * @return 是否发送成功
     */
    public boolean publish(String topic, Object payload, MqttQoS qos, boolean retain) {
        return client.publish(topic, payload, qos, retain);
    }

    /**
     * 发布消息
     *
     * @param topic      topic
     * @param payload    消息体
     * @param qos        MqttQoS
     * @param retain     是否在服务器上保留消息
     * @param properties MqttProperties
     * @return 是否发送成功
     */
    public boolean publish(String topic, Object payload, MqttQoS qos, boolean retain, MqttProperties properties) {
        return client.publish(topic, payload, qos, retain, properties);
    }

    /**
     * 发布消息
     *
     * @param topic   topic
     * @param payload 消息体
     * @param qos     MqttQoS
     * @param builder PublishBuilder
     * @return 是否发送成功
     */
    public boolean publish(String topic, Object payload, MqttQoS qos, Consumer<MqttMessageBuilders.PublishBuilder> builder) {
        return client.publish(topic, payload, qos, builder);
    }

    public void destroy() {
        client.stop();
    }
}
